package vcp.views;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.text.WrappedPlainView;

import vcp.common.AbstractView;
import vcp.common.IView;
import vcp.gui.components.VCPPanel;
import vcp.gui.forms.SubscriptionFormRegular;
import vcp.gui.forms.SubscriptionFormFull;

public class SubscriptionView extends AbstractView implements IView {
    private SubscriptionFormFull subscriptionForm1;
    private SubscriptionFormRegular subscriptionForm2;
	private VCPPanel buttonPanel;
	private VCPPanel switchPanel;
	private VCPPanel wrappPanel;
	private JRadioButton regularBtn;
	private JRadioButton otherBtn;
	
    public SubscriptionView(){
    	wrappPanel =  getWrappedPane();
    }
	@Override
	public void addController(Object obj) {
		subscriptionForm1.getBtnSusbcribe().addActionListener((ActionListener)obj);
		subscriptionForm2.getBtnSusbcribe().addActionListener((ActionListener)obj);	
	}


	public SubscriptionFormFull getSubscriptionForm1() {
		if(subscriptionForm1==null)
			subscriptionForm1 = new SubscriptionFormFull();
		return subscriptionForm1;
	}
	
	public SubscriptionFormRegular getSubscriptionForm2() {
		if(subscriptionForm2==null)
			subscriptionForm2 = new SubscriptionFormRegular();
		return subscriptionForm2;
	}
	
	
	 /* draw and return radio panel */   
    public VCPPanel getButtonPanel(){
    	
		if(buttonPanel==null){
    		buttonPanel = new VCPPanel(new GridLayout(1, 1));
    		buttonPanel.setBackground(Color.white);
    		regularBtn = new JRadioButton("Full Subscription");
    		regularBtn.setBackground(Color.white);
    		regularBtn.setActionCommand("full_subscription");
    		regularBtn.setSelected(true);

    		otherBtn = new JRadioButton("Regular Subscription");
    		otherBtn.setBackground(Color.white);
    		otherBtn.setActionCommand("reg_subscription");
    		otherBtn.setSelected(false);
            
    		/* widget action listener */
    		WrapPanelListener listener = new WrapPanelListener();
    		regularBtn.addActionListener(listener);
    		otherBtn.addActionListener(listener);
    		
    		ButtonGroup btngroup = new ButtonGroup();
    		btngroup.add(regularBtn);
    		btngroup.add(otherBtn);
    		buttonPanel.add(regularBtn);
    		buttonPanel.add(otherBtn);
    	}
    	return buttonPanel;
    }
    
    public VCPPanel getWrappedPane(){
    	subscriptionForm1 = getSubscriptionForm1();
    	subscriptionForm2 = getSubscriptionForm2();
    	buttonPanel = getButtonPanel();
    	wrappPanel = new VCPPanel();
    	switchPanel = new VCPPanel();
    	switchPanel.setLayout(new BorderLayout());
    	switchPanel.add(subscriptionForm1,BorderLayout.CENTER);
    	wrappPanel.setLayout(new BorderLayout());
    	wrappPanel.add(buttonPanel,BorderLayout.PAGE_START);
    	wrappPanel.add(switchPanel,BorderLayout.CENTER);
    	return wrappPanel;
    }
    
    private class WrapPanelListener implements ActionListener{
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		 if(e.getActionCommand().equals("full_subscription")){
    			 switchPanel.removeAll();
    			 switchPanel.add(subscriptionForm1);
    			 switchPanel.revalidate();
    			 switchPanel.repaint();
    		 }else if(e.getActionCommand().equals("reg_subscription")){
    			 switchPanel.removeAll();
    			 switchPanel.add(subscriptionForm2);
    			 switchPanel.revalidate();
    			 switchPanel.repaint();
    		 }
    	}
    }
}


